package com.graduation.auth.api;

import com.base.model.R;
import com.graduation.ucenter.mapper.UserMapper;
import com.graduation.ucenter.model.dto.RegisterDto;
import com.graduation.ucenter.model.po.Authority;
import com.graduation.ucenter.model.po.Role;
import com.graduation.ucenter.model.po.User;
import com.graduation.ucenter.service.AuthorityService;
import com.graduation.ucenter.service.RoleService;
import com.graduation.ucenter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/9/20 15:59
 **/
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthorityService authorityService;

    @RequestMapping("/login-success")
    public String loginSuccess() {
        return "登录成功";
    }

    @RequestMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer Id) {
        User user = userMapper.selectById(Id);
        return user;
    }

    @PostMapping("/register")
    public R<String> register(@RequestBody User user){
        return userService.register(user);
    }

    @PostMapping("/commonRegister")
    public R<String>commonRegister(@RequestBody RegisterDto registerDto){
        registerDto.setRegisterdate(new Timestamp(System.currentTimeMillis()));
        return userService.commonRegister(registerDto);
    }

    @PostMapping("/listUser/{page}/{pageSize}")
    @PreAuthorize("hasAuthority('listUser')")
    public R<List<User>> listUser(@PathVariable Integer page, @PathVariable Integer pageSize, @RequestBody(required = false)User user){
        return userService.listUser(page,pageSize,user);
    }

    @PostMapping("/listRole/{page}/{pageSize}")
    @PreAuthorize("hasAuthority('listRole')")
    public R<List<Role>>listRole(@PathVariable Integer page,@PathVariable Integer pageSize,@RequestBody(required = false)Role role){
        return roleService.listRole(page,pageSize,role);
    }

    @PostMapping("/listAuthority/{page}/{pageSize}")
    @PreAuthorize("hasAuthority('listAuthority')")
    public R<List<Authority>>listAuthority(@PathVariable Integer page,@PathVariable Integer pageSize,@RequestBody(required = false) Authority authority){
        return authorityService.listAuthority(page,pageSize,authority);
    }

    @PostMapping("/disableUser")
    @PreAuthorize("hasAuthority('disableUser')")
    public R<String>disableUser(@RequestBody Integer userId){
        return userService.disableUser(userId);
    }

    @PostMapping("/enableUser")
    @PreAuthorize("hasAuthority('enableUser')")
    public R<String>enableUser(@RequestBody Integer userId){
        return userService.enableUser(userId);
    }

    @PostMapping("/listRoleByUserId")
    @PreAuthorize("hasAuthority('listRoleByUserId')")
    public R<List<Role>>listRoleByUserId(@RequestBody Integer userId){
        return roleService.listRoleByUserId(userId);
    }


    @PostMapping("/addBindRoleByUserId")
    @PreAuthorize("hasAuthority('bindRole')")
    public R<String>addBindRoleByUserId(@RequestParam("roleId")Integer roleId,@RequestParam("userId")Integer userId){
        return userService.addBindRoleByUserId(roleId,userId);
    }

    @PostMapping("/removeBindRoleByUserId")
    @PreAuthorize("hasAuthority('removeBindRole')")
    public R<String>removeBindRoleByUserId(@RequestParam("roleId")Integer roleId,@RequestParam("userId")Integer userId){
        return userService.removeBindRoleByUserId(roleId,userId);
    }


    @PostMapping("/listAuthorityByRoleId")
    @PreAuthorize("hasAuthority('listAuthorityByRoleId')")
    public R<List<Authority>>listAuthorityByRoleId(@RequestBody Integer roleId){
        return roleService.listAuthorityByRoleId(roleId);
    }

    @PostMapping("/removeBindAuthorityByRoleId")
    @PreAuthorize("hasAuthority('removeBindAuthority')")
    public R<String>removeBindAuthorityByRoleId(@RequestParam("roleId")Integer roleId,@RequestParam("authorityId")Integer authorityId){
        return roleService.removeBindAuthorityByRoleId(roleId,authorityId);
    }

    @PostMapping("/addBindAuthorityByRoleId")
    @PreAuthorize("hasAuthority('bindAuthority')")
    public R<String>addBindAuthorityByRoleId(@RequestParam("authorityId")Integer authorityId,@RequestParam("roleId")Integer roleId){
        return roleService.addBindAuthorityByRoleId(authorityId,roleId);
    }

    @PostMapping("/addAuthority")
    @PreAuthorize("hasAuthority('addAuthority')")
    public R<String>addAuthority(@RequestBody Authority authority){
        return authorityService.addAuthority(authority);
    }

    @PostMapping("/removeAuthorityAndBindRole")
    @PreAuthorize("hasAuthority('removeAuthority')")
    public R<String>removeAuthorityAndBindRole(@RequestBody Integer authorityId){
        return authorityService.removeAuthorityAndBindRole(authorityId);
    }

    @PostMapping("/addRole")
    @PreAuthorize("hasAuthority('addRole')")
    public R<String>addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }


    @PostMapping("/addUser")
    @PreAuthorize("hasAuthority('addUser')")
    public R<String>addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    @PostMapping("/test")
    public R<List<User>>test(){
        int i=1/0;
        return null;
    }
}
